package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeOpermonRtPv;
import com.nari.ncps.screen.entity.ne.NeOpermonRtWind;
import com.nari.ncps.screen.mapper.ne.*;
import com.nari.ncps.screen.service.ne.NeOpermonRtLoadService;
import com.nari.ncps.screen.vo.ne.NeRealTimeLoadVO;
import com.nari.ncps.screen.entity.ne.NeOpermonRtLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.NumberUp;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 电厂总实时负荷表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonRtLoadServiceImpl implements NeOpermonRtLoadService {
    @Autowired
    private NeOpermonRtWindMapper neOpermonRtWindMapper;

    @Autowired
    private NeOpermonRtPvMapper neOpermonRtPvMapper;

    @Override
    public List<NeRealTimeLoadVO> getRealTimeLoad(Integer year) {
        String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 构建查询条件 - 查询最新一天的24小时数据
        LambdaQueryWrapper<NeOpermonRtWind> windWrapper = new LambdaQueryWrapper<>();
        windWrapper.eq(NeOpermonRtWind::getDelFlag, 0);
//        windWrapper.like(NeOpermonRtWind::getStatisticTime, "2025-09-29" );
        windWrapper.like(NeOpermonRtWind::getStatisticTime, formattedDate );
        List<NeOpermonRtWind> windDataList = neOpermonRtWindMapper.selectList(windWrapper);


        // 构建查询条件 - 查询最新一天的24小时数据
        LambdaQueryWrapper<NeOpermonRtPv> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeOpermonRtPv::getDelFlag, 0 );
//        queryWrapper.like(NeOpermonRtPv::getStatisticTime, "2025-10-06" );
        queryWrapper.like(NeOpermonRtPv::getStatisticTime, formattedDate );
        // 先查询最新的日期
        List<NeOpermonRtPv> pvDataList = neOpermonRtPvMapper.selectList(queryWrapper);

        List<NeRealTimeLoadVO> voList=new ArrayList<>();
        if (pvDataList != null && !pvDataList.isEmpty() && windDataList != null && !windDataList.isEmpty()) {
            for(int i=0;i<pvDataList.size();i++){
                for (int j=0;j<windDataList.size();j++){
                    Byte pvHour = pvDataList.get(i).getHour();
                    Byte windHour = windDataList.get(j).getHour();
                    Byte pvMinute = pvDataList.get(i).getMinute();
                    Byte windMinute = windDataList.get(j).getMinute();

                    boolean hourMatch = (pvHour == null && windHour == null) ||
                            (pvHour != null && pvHour.equals(windHour));
//                    boolean minuteMatch = (pvMinute == null && windMinute == null) ||
//                            (pvMinute != null && pvMinute.equals(windMinute));

                    if(hourMatch ){
                        pvDataList.get(i).setRealtimePower(pvDataList.get(i).getRealtimePower().add(windDataList.get(j).getRealtimePower()));
                        pvDataList.get(i).setTheoreticalPower(pvDataList.get(i).getTheoreticalPower().add(windDataList.get(j).getTheoreticalPower()));
                        NeRealTimeLoadVO vo = new NeRealTimeLoadVO ();
                        vo.setYear(pvDataList.get(i).getYear());
                        vo.setMonth(pvDataList.get(i).getMonth());
                        vo.setDay(pvDataList.get(i).getDay());
                        if(pvDataList.get(i).getHour()!=null&& pvDataList.get(i).getHour()>0 ){
                            Integer integerValue = Integer.valueOf(pvDataList.get(i).getHour());
                            vo.setHour(integerValue);
                            vo.setMinute(0);
                        }
                        vo.setRealtimeLoad(pvDataList.get(i).getRealtimePower());
                        voList.add(vo);
                    }
                }
            }
        }else{
            NeRealTimeLoadVO vo = new NeRealTimeLoadVO ();
            vo.setYear(0);
            vo.setMonth(0);
            vo.setDay(0);
            vo.setHour(0);
            vo.setRealtimeLoad(BigDecimal.ZERO);
            voList.add(vo);
        }
        // 转换为VO对象
//        return entities.stream().map(this::convertToVO).collect(Collectors.toList());
        return voList;
    }

    /**
     * 实体类转换为VO
     */
    private NeRealTimeLoadVO convertToVO(NeOpermonRtLoad entity) {
        NeRealTimeLoadVO vo = new NeRealTimeLoadVO();
        vo.setYear(entity.getYear());
        vo.setMonth(entity.getMonth());
        vo.setDay(entity.getDay());
        vo.setDate(entity.getDate());
        vo.setHour(entity.getHour() != null ? entity.getHour().intValue() : null);
        vo.setMinute(entity.getMinute() != null ? entity.getMinute().intValue() : null);
        vo.setRealtimeLoad(entity.getRealtimeLoad());
        return vo;
    }

    @Override
    public boolean saveBatch(Collection<NeOpermonRtLoad> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<NeOpermonRtLoad> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<NeOpermonRtLoad> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(NeOpermonRtLoad entity) {
        return false;
    }

    @Override
    public NeOpermonRtLoad getOne(Wrapper<NeOpermonRtLoad> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<NeOpermonRtLoad> getOneOpt(Wrapper<NeOpermonRtLoad> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<NeOpermonRtLoad> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<NeOpermonRtLoad> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<NeOpermonRtLoad> getBaseMapper() {
        return null;
    }

    @Override
    public Class<NeOpermonRtLoad> getEntityClass() {
        return null;
    }
}
